﻿using Microsoft.AspNetCore.Mvc;
using Zhp.Api.Attributes;
using Zhp.IService.Message.Email;
using Zhp.Model.Common;
using Zhp.Model.Message.Email;

namespace Zhp.Api.Controllers.Message.Email
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiGroup(GroupName = ApiGroupEnum.Message)]
    [SeedMenu(MenuGroup = ApiGroupEnum.Message, Name = "邮件模板管理", Order = 2, RouterName = "emailtemplate", WebFilePath = "message/email/emailTemplate/index", Icon = "message-tpl")]
    public class EmailTemplateController : ControllerBase
    {
        protected readonly IEmailTemplateService _emailTemplateService;
        protected readonly IEmailTemplateRender _emailTemplateRender;
        public EmailTemplateController(IEmailTemplateService emailTemplateService, IEmailTemplateRender emailTemplateRender)
        {
            _emailTemplateService = emailTemplateService;
            _emailTemplateRender = emailTemplateRender;
        }

        [HttpPost]
        [SeedList("邮件模板列表", "邮件模板管理", ApiGroupEnum.Message)]
        public async Task<PageRet<EmailTemplateModel>> Query(QueryEmailTemplateParam p)
        {
            var list = await _emailTemplateService.Query(p);
            return new PageRet<EmailTemplateModel>
            {
                DataList = list,
                TotalCount = p.TotalCount,
            };
        }

        [HttpPost]
        [SeedButton("新增", "邮件模板列表", "邮件模板管理", ApiGroupEnum.Message, ["/api/EmailTemplate/QueryVariableClass", "/api/EmailTemplate/QueryVariable", "/api/EmailAccount/QueryAccountSelect"])]
        public async Task<bool> Create(EditEmailTemplateParam p)
        {
            return await _emailTemplateService.Create(p);
        }

        [HttpPut]
        [SeedButton("修改", "邮件模板列表", "邮件模板管理", ApiGroupEnum.Message, ["/api/EmailTemplate/QueryVariableClass", "/api/EmailTemplate/QueryVariable", "/api/EmailAccount/QueryAccountSelect"])]
        public async Task<bool> Update(EditEmailTemplateParam p)
        {
            return await _emailTemplateService.Update(p);
        }

        [HttpDelete("{id}")]
        [SeedButton("删除", "邮件模板列表", "邮件模板管理", ApiGroupEnum.Message)]
        public async Task<bool> Delete(long id)
        {
            return await _emailTemplateService.Delete(id);
        }

        [HttpPost]
        [SeedButton("测试发送:发送邮件", "邮件模板列表", "邮件模板管理", ApiGroupEnum.Message)]
        public async Task<bool> TestSendEmail(TestSendEmailParam p)
        {
            return await _emailTemplateService.TestSendEmail(p);
        }

        [HttpGet]
        [SeedButton("导出", "邮件模板列表", "邮件模板管理", ApiGroupEnum.Message)]
        public async Task<IActionResult> ExportExcel()
        {
            var (stream, contentType, fileName) = await _emailTemplateService.ExportExcel();

            return File(stream, contentType, fileName);
        }

        [HttpPost]
        [SeedButton("导入", "邮件模板列表", "邮件模板管理", ApiGroupEnum.Message)]
        public async Task<bool> ImportExcel(IFormFile file)
        {
            return await _emailTemplateService.ImportExcel(file.OpenReadStream());
        }

        [HttpGet]
        public async Task<List<TemplateVariableModel>> QueryVariableClass()
        {
            return await _emailTemplateRender.QueryVariableClass();
        }

        [HttpGet]
        public async Task<List<TemplateVariableModel>> QueryVariable(string variableClass)
        {
            return await _emailTemplateRender.QueryVariable(variableClass);
        }

        [HttpGet]
        [SeedButton("测试发送", "邮件模板列表", "邮件模板管理", ApiGroupEnum.Message)]
        public async Task<TemplateVariableJson> QueryDefaultJson(string variableClass)
        {
            return await _emailTemplateRender.QueryDefaultJson(variableClass);
        }
    }
}
